﻿// -----------------------------------------------------------------------
// <copyright file="IMG_GeometricMeanFilter.cs" company="Hewlett-Packard">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ImageProcessing.ImageProcessor
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// IMG_GeometricMeanFilter class
    /// </summary>
    public static class IMG_GeometricMeanFilter
    {
        /// <summary>
        /// apply geometric filter.
        /// </summary>
        /// <param name="img_Original">The img_ original.</param>
        /// <param name="img_WindowSize">Size of the img_ window.</param>
        /// <returns>image after geometric mask</returns>
        public static Entities.IMG_Image IMG_ApplyGeometricFilter(Entities.IMG_Image img_Original, int img_WindowSize)
        {
            try
            {
                int img_Width = img_Original.Width;
                int img_Height = img_Original.Height;
                int img_Padding = img_WindowSize / 2;
                int img_newWidth = img_Width + (2 * img_Padding);
                int img_newHeight = img_Height + (2 * img_Padding);
                Entities.IMG_Image img_TempBuffer = new Entities.IMG_Image(img_newWidth, img_newHeight);
                Entities.IMG_Image img_ReturnBuffer = new Entities.IMG_Image(img_Width, img_Height);
                for (int i = 0; i < img_newHeight * img_newWidth; i++)
                {
                    img_TempBuffer.ImagePixels[i] = new Entities.Pixel(1, 1, 1);
                }

                for (int i = img_Padding; i < img_newWidth - img_Padding; i++)
                {
                    for (int j = img_Padding; j < img_newHeight - img_Padding; j++)
                    {
                        img_TempBuffer[j, i] = img_Original[j - img_Padding, i - img_Padding];
                    }
                }

                for (int dim_I = 0; dim_I < img_Height; dim_I++)
                {
                    for (int dim_J = 0; dim_J < img_Width; dim_J++)
                    {
                        double img_Red = 1, img_Green = 1, img_Blue = 1;
                        for (int msk_I = 0; msk_I < img_WindowSize; msk_I++)
                        {
                            for (int msk_J = 0; msk_J < img_WindowSize; msk_J++)
                            {
                                img_Red *= img_TempBuffer[dim_I + msk_I, dim_J + msk_J].Red == 0 ? 1 : img_TempBuffer[dim_I + msk_I, dim_J + msk_J].Red;
                                img_Green *= img_TempBuffer[dim_I + msk_I, dim_J + msk_J].Green == 0 ? 1 : img_TempBuffer[dim_I + msk_I, dim_J + msk_J].Green;
                                img_Blue *= img_TempBuffer[dim_I + msk_I, dim_J + msk_J].Blue == 0 ? 1 : img_TempBuffer[dim_I + msk_I, dim_J + msk_J].Blue;
                            }
                        }

                        double img_power = 1.0 / (img_WindowSize * img_WindowSize);
                        img_ReturnBuffer[dim_I, dim_J] = new Entities.Pixel(Math.Pow(img_Red, img_power), Math.Pow(img_Green, img_power), Math.Pow(img_Blue, img_power));
                    }
                }

                return img_ReturnBuffer;
            }
            catch (Exception img_EX)
            {
                Logger.LogException(img_EX);
                throw;
            }
        }
    }
}
